package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.mapper.CourseCategoryMapper;
import com.xuecheng.content.model.dto.CourseCategoryTreeDto;
import com.xuecheng.content.model.po.CourseCategory;
import com.xuecheng.content.service.CourseCategoryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 徐晨标
* @description 针对表【course_category(课程分类)】的数据库操作Service实现
* @createDate 2023-07-07 09:14:18
*/
@Service
public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper, CourseCategory>
    implements CourseCategoryService {
    @Override
    public List<CourseCategoryTreeDto> combinationDepart() {
        List<CourseCategory> categoryList = this.list();
        List<CourseCategoryTreeDto> nodeList = new ArrayList<>();
        for (CourseCategory courseCategory : categoryList) {
            nodeList.add(new CourseCategoryTreeDto(courseCategory.getId(),
                    courseCategory.getName(),
                    courseCategory.getLabel(),
                    courseCategory.getParentid(),
                    courseCategory.getIsShow(),
                    courseCategory.getOrderby(),
                    courseCategory.getIsLeaf()));
        }
        List<CourseCategoryTreeDto> hierarchicalNodes = CourseCategoryTreeDto.buildHierarchy(nodeList);

      deleteNullChildren(hierarchicalNodes);

        return hierarchicalNodes;
    }

    private void deleteNullChildren(List<CourseCategoryTreeDto> hierarchicalNodes) {
        for (CourseCategoryTreeDto hierarchicalNode : hierarchicalNodes) {
            if (hierarchicalNode.getChildrenTreeNodes().size()==0){
                hierarchicalNode.setChildrenTreeNodes(null);
            }else {
                deleteNullChildren(hierarchicalNode.getChildrenTreeNodes());
            }
        }
    }


}




